package com.windystory.wslibrary.ui.edittext;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;

import com.windystory.wslibrary.R;


/**
 * EditText
 *
 * @author windystory
 * @version 1.0
 */
public class EditText extends androidx.appcompat.widget.AppCompatEditText {

    private static final int UNSPECIFIED_INT = Integer.MAX_VALUE;

    //放大镜位置 左
    private final int LEFT = 0;

    //正常类型
    private final int NORMAL = 0;
    //搜索类型
    private final int SEARCH = 1;

    //圆角矩形圆角角度大小dp
    private int radianEt = 15;
    //边框大小
    private int border = 1;
    //边框颜色
    private int borderColorEt = 0;
    //搜索放大镜位置
    private int searchLocation;

    public EditText(Context context) {
        super(context);
    }

    public EditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttrs(context, attrs);
    }


    public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context, attrs);
    }

    private void getAttrs(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditText);
            borderColorEt = a.getColor(R.styleable.EditText_borderColorEt, UNSPECIFIED_INT);
            border = a.getInt(R.styleable.EditText_borderEt, 1);
            radianEt = a.getInt(R.styleable.EditText_radianEt, 15);
            searchLocation = a.getInt(R.styleable.EditText_searchLocation, LEFT);
            int EDIT_TEXT_STYLE = a.getInt(R.styleable.EditText_styleEditText, NORMAL);
            setEditText(EDIT_TEXT_STYLE);
        }
    }

    /**
     * 设置EditText样式
     *
     * @param editTextStyle
     */
    private void setEditText(int editTextStyle) {
        switch (editTextStyle) {
            case NORMAL:
                GradientDrawable normal = drawBase();
                StateListDrawable stateListDrawable = new StateListDrawable();
                stateListDrawable.addState(new int[]{}, normal);
                setBackgroundDrawable(stateListDrawable);
                break;
            case SEARCH:
                GradientDrawable searchDrawable = drawBase();
                StateListDrawable stateListDrawableDrawable = new StateListDrawable();
                stateListDrawableDrawable.addState(new int[]{}, searchDrawable);
                setBackgroundDrawable(stateListDrawableDrawable);
                Drawable search = getResources().getDrawable(R.drawable.pic_search);

                if (searchLocation == LEFT) {
                    search.setBounds(0, 0, search.getMinimumWidth(),
                            search.getMinimumHeight());
                    setCompoundDrawables(search, null, null, null);
                } else {
                    search.setBounds(0, 0, search.getMinimumWidth(),
                            search.getMinimumHeight());
                    setCompoundDrawables(null, null, search, null);
                }
        }
    }

    /**
     * drawBase
     *
     * @return
     */
    private GradientDrawable drawBase() {
        GradientDrawable normal = new GradientDrawable();
        normal.setColor(Color.WHITE);
        normal.setCornerRadius(radianEt);
        normal.setStroke(border, borderColorEt);
        return normal;
    }

    /**
     * 设置密码可见
     */
    public void setPassWordVisable() {
        setTransformationMethod(HideReturnsTransformationMethod.getInstance());
    }

    /**
     * 设置密码不可见
     */
    public void setPasswordInVisable() {
        setTransformationMethod(PasswordTransformationMethod.getInstance());
    }


}
